package cn.tedu.smartos.elderly.service.impl;

import cn.tedu.smartos.base.exception.ServiceException;
import cn.tedu.smartos.elderly.mapper.ElderlyMapper;
import cn.tedu.smartos.elderly.pojo.dto.ElderlyAddParam;
import cn.tedu.smartos.elderly.pojo.dto.ElderlyUpdateParam;
import cn.tedu.smartos.elderly.pojo.dto.MedicationAddParam;
import cn.tedu.smartos.elderly.pojo.dto.MedicationUpdateParam;
import cn.tedu.smartos.elderly.pojo.entity.Elderly;
import cn.tedu.smartos.elderly.pojo.entity.MedicationReminder;
import cn.tedu.smartos.elderly.pojo.vo.ElderlyVO;
import cn.tedu.smartos.elderly.pojo.vo.MedicationReminderVO;
import cn.tedu.smartos.elderly.service.ElderlyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ElderlyServiceImpl implements ElderlyService {
    @Autowired
    private ElderlyMapper elderlyMapper;

    /**
     * 获取老人信息
     *
     * @param userId 用户ID
     * @return 老人信息
     */
    @Override
    public ElderlyVO getInfo(Long userId) {
        log.debug("开始处理【获取老人信息】的业务，参数：{}", userId);
        if (userId == null){
            throw new ServiceException("参数userId不能为空");
        }
        ElderlyVO elderlyVO = elderlyMapper.getInfo(userId);
        return elderlyVO;
    }
    /**
     * 添加老人信息
     * @param elderlyCreateParam
     */
    @Override
    public void addInfo(ElderlyAddParam elderlyCreateParam) {
        log.debug("开始处理【添加老人信息】的请求,参数{}", elderlyCreateParam);
        //  创建Elderly对象
        Elderly elderly = new Elderly();
        BeanUtils.copyProperties(elderlyCreateParam, elderly);
        elderly.setCreateTime(new Date());
        elderly.setUpdateTime(new Date());
        //  调用Mapper执行插入
        Integer rows = elderlyMapper.insertInfo(elderly);
        if (rows != 1) {
            throw new ServiceException("添加失败");
        }
    }
    /**
     * 修改老人信息
     * @param elderlyUpdateParam
     */
    @Override
    public void updateInfo(ElderlyUpdateParam elderlyUpdateParam) {
        log.debug("开始处理【修改老人信息】的请求,参数{}", elderlyUpdateParam);
        Elderly elderly = new Elderly();
        BeanUtils.copyProperties(elderlyUpdateParam, elderly);
        elderly.setUpdateTime(new Date());
        Integer rows = elderlyMapper.updateInfo(elderly);
        if (rows != 1) {
            throw new ServiceException("修改失败");
        }
    }

//————————————————————————————————————————————————————————————————————————————————

    /**
     * 用药提醒
     * @param elderlyUserId
     * @param pageNum
     * @param pageSize
     * @return PageInfo<MedicationReminderVO>
     */
    @Override
    public PageInfo<MedicationReminderVO> medicineReminder(Long elderlyUserId,Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查看老人用药提醒】的请求,参数{},{},{}", elderlyUserId, pageNum, pageSize);
        if (elderlyUserId == null){
            throw new ServiceException("参数elderlyUserId不能为空");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MedicationReminderVO> list = elderlyMapper.medicineReminder(elderlyUserId);
        return new PageInfo<>(list);
    }
    /**
     * 用药提醒详情
     * @param reminderId
     * @return MedicationReminderVO
     */
    @Override
    public MedicationReminderVO medicineReminderDetail(Long reminderId) {
        log.debug("开始处理【查看老人用药提醒详情】的请求,参数{}", reminderId);
        if (reminderId == null){
            throw new ServiceException("参数reminderId不能为空");
        }
        MedicationReminderVO medicationReminderVO = elderlyMapper.medicineReminderDetail(reminderId);
        return medicationReminderVO;
    }
    /**
     * 删除用药提醒
     * @param reminderIds
     */
    @Override
    public void deleteMedicineReminder(List<Long> reminderIds) {
        log.debug("开始处理【删除用药提醒】的请求,参数{}", reminderIds);
        if (CollectionUtils.isEmpty(reminderIds)){
            throw new ServiceException("参数reminderIds不能为空");
        }
        Integer rows = elderlyMapper.deleteMedicineReminder(reminderIds);
        if (rows == 0){
            throw new ServiceException("删除出错");
        }
        if (rows != reminderIds.size()){
            throw new ServiceException("部分用药提醒删除失败");
        }
    }
    /**
     * 添加用药提醒
     * @param addParam
     */
    @Override
    public void addMedicineReminder(MedicationAddParam addParam) {
        log.debug("开始处理【添加用药提醒】的请求,参数{}", addParam);
        MedicationReminder medicationReminder = new MedicationReminder();
        BeanUtils.copyProperties(addParam, medicationReminder);
        medicationReminder.setCreateTime(new Date());
        medicationReminder.setUpdateTime(new Date());
        Integer rows = elderlyMapper.addMedicineReminder(medicationReminder);
        if (rows != 1){
            throw new ServiceException("添加失败");
        }
    }
    /**
     * 修改用药提醒
     * @param updateParam
     */
    @Override
    public void updateMedicineReminder(MedicationUpdateParam updateParam) {
        log.debug("开始处理【修改用药提醒】的请求,参数{}", updateParam);
        MedicationReminder medicationReminder = new MedicationReminder();
        BeanUtils.copyProperties(updateParam, medicationReminder);
        medicationReminder.setUpdateTime(new Date());
        Integer rows = elderlyMapper.updateMedicineReminder(medicationReminder);
        if (rows != 1){
            throw new ServiceException("修改失败");
        }
    }
}
